package com.changgou.order.controller;

import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import entity.Result;
import entity.StatusCode;
import entity.TokenDecode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 添加购物车
     * @param id
     * @param num
     * @return
     */
    @GetMapping("/add")
    public Result add(Long id, Integer num){
        Map<String, String> userInfo = TokenDecode.getUserInfo();
        System.out.println(userInfo);
        String username = userInfo.get("username");
        cartService.add(id,num,username);
        return new Result(true, StatusCode.OK,"添加购物车成功！");
    }

    @GetMapping("/list")
    public Result<List<OrderItem>> list(){
        Map<String, String> userInfo = TokenDecode.getUserInfo();
        System.out.println(userInfo);
        String username = userInfo.get("username");
        List<OrderItem> orderItems = cartService.list(username);
        return new Result(true, StatusCode.OK,"查询购物车成功！",orderItems);
    }

}
